package com.controller;

import com.mapper.StudentMapper;
import com.mapper.UserMapper;
import com.pojo.Admin;
import com.pojo.Teacher;
import com.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserMapper um;
    @Autowired
    StudentMapper sm;

    /**
     * 用户登录
     * @param role 角色码 0-管理员 1-教师
     * @param account 账号
     * @param password 密码
     */
    @RequestMapping("/login")
    public Map<String,Object> login(Integer role,String account,String password) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (role == null) {
            code = 500;
            msg = "角色码不能为空";
        } else if (Tool.isNull(account)) {
            code = 501;
            msg = "账号不能为空";
        } else if (Tool.isNull(password)) {
            code = 502;
            msg = "密码不能为空";
        } else {
            Admin admin = um.getAdminByAccount(account);
            Teacher teacher = um.getTeacherByAccount(account);
            if (role == 0 && admin == null || role == 1 && teacher == null) {
                code = 503;
                msg = "账号不存在";
            } else if (role == 0 && admin.getPassword().equals("123456") || role == 1 && teacher.getPassword().equals("123456")) {
                code = 504;
                msg = "密码错误";
            } else {
                if (role == 0) {
                    um.upAdminLoginTime(admin.getId());
                    admin.setPassword(null);
                    map.put("data",admin);
                } else {
                    um.upTeacherLoginTime(teacher.getId());
                    teacher.setPassword(null);
                    map.put("data",teacher);
                }
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改用户名
     * @param role 角色码 0-管理员 1-教师
     * @param id 用户 id
     * @param name 新的用户名
     */
    @RequestMapping("/editName")
    public Map<String,Object> editUserName(Integer role,Integer id,String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (role == null) {
            code = 500;
            msg = "角色码不能为空";
        } else if (id == null) {
            code = 501;
            msg = "用户 id 不能为空";
        } else if (um.getAdminById(id) == null && um.getTeacherById(id) == null) {
            code = 502;
            msg = "用户不存在";
        } else if (name == null) {
            code = 503;
            msg = "用户名不能为空";
        } else if (role == 0 && um.getAdminByName(name).size() != 0) {
            code = 504;
            msg = "用户名已存在";
        } else {
            if (role == 0) um.editAdminName(id,name);
            else um.editTeacherName(id, name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }


    /**
     * 修改账号
     * @param role 角色码 0-管理员 1-教师
     * @param id 用户 id
     * @param account 账号
     */
    @RequestMapping("/editAccount")
    public Map<String,Object> editAccount(Integer role,Integer id,String account) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (role == null) {
            code = 500;
            msg = "角色码不能为空";
        } else if (id == null) {
            code = 501;
            msg = "用户 id 不能为空";
        } else if (role == 0 && um.getAdminById(id) == null || role == 1 && um.getTeacherById(id) == null) {
            code = 502;
            msg = "用户不存在";
        } else if (account == null) {
            code = 503;
            msg = "账号不能为空";
        } else if (role == 0 && um.getAdminByAccount(account) != null || role == 1 && um.getTeacherByAccount(account) != null) {
            code = 504;
            msg = "账号已存在";
        } else {
            if (role == 0) um.updateAdminAccount(id, account);
            else um.updateTeacherAccount(id, account);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改头像
     * @param role 角色码 0-管理员 1-教师 2-学生
     * @param open_id 用户 id
     * @param avatar 头像
     */
    @RequestMapping("/editAvatar")
    public Map<String,Object> editAvatar(Integer role,String open_id,String avatar) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (role == null) {
            code = 500;
            msg = "角色码不能为空";
        } else if (open_id == null) {
            code = 501;
            msg = "用户 id 不能为空";
        } else if (role == 0 && um.getAdminByOpenId(open_id) == null || role == 1 && um.getTeacherByOpenId(open_id) == null) {
            code = 502;
            msg = "用户不存在";
        } else if (avatar == null) {
            code = 503;
            msg = "头像不能为空";
        } else {
            if (role == 0) um.updateAdminAvatar(open_id, avatar);
            else um.updateTeacherAvatar(open_id, avatar);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取用户信息
     * @param role 角色码 0-管理员 1-教师
     * @param id 用户 id
     */
    @RequestMapping("/getUserInfo")
    public Map<String,Object> getUserInfo(Integer role,Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (role == null) {
            code = 500;
            msg = "角色码不能为空";
        } else if (id == null) {
            code = 501;
            msg = "用户 id 不能为空";
        } else {
            Admin admin = um.getAdminById(id);
            Teacher teacher = um.getTeacherById(id);
            if (role == 0 && admin == null || role == 1 && teacher == null) {
                code = 502;
                if(role==0) msg = "用户不存在";
                else if(role==1) msg="该教师未注册,请联系管理员输入手机号登录!";
            } else {
                if (admin != null) admin.setPassword(null);
                if (teacher != null) {
                    teacher.setPassword(null);
                    teacher.setStation_name(um.getStationNameById(teacher.getStation_id()));
                }
                map.put("data",role == 0 ? admin : teacher);
            }
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }


}
